import { Controller, Get, Post, Body, ParseIntPipe,Patch, Param, Delete, Query, UseInterceptors, UploadedFiles } from '@nestjs/common';
import { AnyFilesInterceptor } from '@nestjs/platform-express';
import { PersonService } from './person.service';

//DTO (data transfer object) - 就是用于封装传输的数据的对象
import { CreatePersonDto } from './dto/create-person.dto';

@Controller('api/person')
export class PersonController {
  constructor(private readonly personService: PersonService) {}

  //#url query(GET)  |  @Controller 和 @Get()的路由会拼到一起，通过/api/person/xxx 才能访问到这个方法
  @Get('find')
  query(@Query('name') name:string,@Query('age') age:number){
    return `received: name=${name},age=${age}`;
  } 


  //form urlencoded(POST)
  @Post('post')
  body(@Body() createPersonDto: CreatePersonDto) {
    return `received: ${JSON.stringify(createPersonDto)}`
  }
  

  //pnpm i -D @types/multer
  //TODO: 如何保留后缀名? 或者限制上传格式?
  @Post('file')
  @UseInterceptors(AnyFilesInterceptor({  //启用FilesInterceptor拦截器
     dest: 'uploads/'
  }))
  body2(@Body() createPersonDto: CreatePersonDto, @UploadedFiles() files: Array<Express.Multer.File>) {
    console.log(files);
    return `received: ${JSON.stringify(createPersonDto)}`
  }


  //#url param（GET）
  @Get(':id')  
  findOne(@Param('id') id: string) {
    return `received: id=${id}`;
    // return this.personService.findOne(+id);
  }

  //获得query参数,并用管道验证整形
  @Get('hello')
  getHello5(@Query('aaa',new ParseIntPipe()) aaa){
      console.log(aaa);
  }

}
